using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace TST.SharePoint.Shared
{
    public class WSSFieldMultiChoice : Field
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public WSSFieldMultiChoice()
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ID">The ID for the field.</param>
        public WSSFieldMultiChoice(string Id)
            : base(Id)
        {
        }

        public override object GetTypedValue(object value)
        {
            if (value == null)
                return null;
            return ConvertMultiChoiceToValue(value.ToString());
        }

        /// <summary>
        /// Multi choice fields have their selected values stored as 1 string like "option1;#option3;#option22".
        /// Return this as "option1; option3; option22", just like SharePoint does.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string ConvertMultiChoiceToValue(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
            if (value.StartsWith(";#"))
                value = value.Substring(2);
            if (value.EndsWith(";#"))
                value = value.Substring(0, value.Length - 2);
            return value.Replace(";#", "; ");
        }


    }
}
